/**
 *  题库相关的业务处理
 *  1  题库分类树展示以及维护
 *  2  分类下的题库展示
 *  3
 *
 * @type {exports}
 */
var express = require('express');
var mongoose = require('mongoose');
var async = require('async');
var url = require('url');
var mongoose = require('mongoose');
var util = require('../models/util');

require('../models/category.js');
require('../models/store.js');
var Category = mongoose.model('Category');
var Store = mongoose.model('Store');

var router = express.Router();

//显示题目树结构
router.post('/subcategory', function (req, res) {
    var query = url.parse(req.url, true).query;
    console.log(query);
    var pid = query.pid;
    Category.find({
        parent: pid == 'root' ? undefined : pid
    }).exec(function (err, categorys) {
        if (err) throw err;
        res.send(categorys);
    });
});

//题库
router.post('/stores', function(req, res){
    var query = req.query;
    console.log(query);

    var categoryId = query.categoryId;
    var skip = query.skip || 0;
    var limit = query.limit || 5;
    Store.find({
        category : categoryId,
        active: true
    }).skip(skip).limit(limit).exec(function(err, data){
        console.log(data);
        res.send(data);
    });
});

module.exports = router;
